


var Area_Code = GetQueryVariable("Area_Code")+"";
var _Area_Code = Area_Code.replace(/^\s+|\s+$/g,"");
//alert(Area_Code);
var lnglat = [108.915376, 34.244658];
if(_Area_Code == "1")
{
	lnglat = [108.765186, 34.028397];
}
var map = new AMap.Map('container', {
	resizeEnable: true,
	zoom:17,
	zooms:[15,20],
	center: lnglat,
	mapStyle:'amap://styles/a92c23c68ca519ec235825c3be99462b'
	//前往创建自定义地图样式：https://lbs.amap.com/dev/mapstyle/index
});

var num = 0;
var clickListener = AMap.event.addListener(map, "click", mapOnClick);
var pointsArray;
var marksArray;
var polygonEditor;	
var polygon;

var polygonDic = {};
var polygonEditorDic = {};

function init()
{
	marksArray =new Array();
	num = 0;
	pointsArray =new Array();
}
function mapOnClick(e) {
     //console.log(playerUIApp.isDraw);
	if(playerUIApp.isDraw == false)
	{
		return ;
	}
	var result = CheckValid(e.lnglat);
	// console.log(result);
	if(result)
	{
		layer.alert("区域不能出现重合，请重试！");
		return ;
	}
	if(num<3)
	{
		marksArray.push(addMarker(e.lnglat));
		pointsArray.push(e.lnglat);
	}
     
      num++;
      if(num == 3){
          //AMap.event.removeListener(clickListener);
          polygon = createPolygon(0,pointsArray,playerUIApp.isDrawType?alarmColor:normalColor);
          polygonEditor = createEditor(0,polygon);
          clearMarks();
      }
		
 };

 function addMarker(lnglat) {
        
        var marker = new AMap.Marker({
            icon: "http://webapi.amap.com/theme/v1.3/markers/n/mark_b.png",
            position: lnglat
        });
        marker.setMap(map);
        return marker;
  }
  function clearMarks(){
      map.remove(marksArray);
  }
  
 function createPolygon(Area_ID,_pointsArray,color){
     var polygon = new AMap.Polygon({
         map: map,
         path: _pointsArray,
         strokeColor: color,
         strokeOpacity: 1,
         strokeWeight: 3,
         fillColor: "#f5deb3",
         fillOpacity: 0.35
     });
	 
	AMap.event.addListener(polygon, "dblclick", PolygonDbOnClick);
	AMap.event.addListener(polygon, "rightclick", PolygonRightOnClick);
	polygon.Area_ID= Area_ID;
	polygonDic[Area_ID] = polygon;
	//console.log(polygon);
     return polygon;
 }
 
 //双击修改
 function PolygonDbOnClick(e)
 {
	 if(playerUIApp.isDraw)
	 {
	 	return ;
	 }
	 playerUIApp.isDraw = true;
	 playerUIApp.buttonTxt = "完成绘制";
	 polygon = e.target;
	 polygonEditor = polygonEditorDic[e.target.Area_ID];
	 polygonEditor.open();
 }
 
 function PolygonRightOnClick(e)
 {
	 if(playerUIApp.isDraw)
	 {
	 	return ;
	 }
	  layer.confirm('确定要删除此区域吗', function(index){
			var target = e.target;
			var Area_ID = target.Area_ID;
			Delete(Area_ID);
			delete polygonDic[target.Area_ID];
			delete polygonEditorDic[target.Area_ID];
			map.remove(target);
			layer.close(index); 
			
	  });
 }
 
 function createEditor(Area_ID,polygon){
     var polygonEditor = new AMap.PolyEditor(map, polygon);
     polygonEditor.open();
     AMap.event.addListener(polygonEditor,'end',polygonEnd);
	 polygonEditorDic[Area_ID] = polygonEditor;
     return polygonEditor;
 }
 
 function closeEditPolygon(){
	 if(CheckEmpty(polygonEditor))
	 {
		 return ;
	 }
	var json = JSON.stringify(polygon.getPath());
	//保存回调
	if(polygon.Area_ID==0)
	{
		Save(json);
	}
	else
	{
		Edit(polygon.Area_ID,json);
	}
	//console.log(JSON.stringify( polygon.getPath()));
     polygonEditor.close();
	 layer.msg('在绘制区域内鼠标双击可再次编辑，单击右键可删除区域。', {
	   icon: 1,
	   time: 5000 //2秒关闭（如果不配置，默认是3秒）
	 }, function(){
	   //do something
	 });  
	 //保存操作
 }
 
 function SaveCallBack(newID)
 {
	  delete polygonEditorDic[0];
	  delete polygonDic[0];
	  polygonEditorDic[newID] = polygonEditor;
	  polygonDic[newID] = polygon;
	  polygon.Area_ID = newID;
	 
 }
		
function polygonEnd(res){
      
       
 }
 
 //区域不能出现交叉
 function CheckValid(point)
 {
	 for(var item in polygonDic)
	 {
		 var _polygon =polygonDic[item];
		 if(_polygon.contains(point))
		 {
			 return true;
		 }
	 }
	 
	 return false;
 }
					
var playerUIApp = new Vue({
	el: '#mapApp',
	data: {
		isDraw: false,
		buttonTxt:"开始绘制",
		isDrawType: false,
		drawTypeTxt:"任务区域"

	},
	mounted() {
		//init();
	},
	methods: {
		Draw() {
			
			this.isDraw = !this.isDraw;
			
			//开始绘制
			if(this.isDraw)
			{
				this.buttonTxt = "完成绘制";
				init();
			}
			else
			{
				this.buttonTxt = "开始绘制";
				closeEditPolygon();
			}
			
		},
		DrawType() {
			
			this.isDrawType = !this.isDrawType;
			//开始绘制
			if(this.isDrawType)
			{
				this.drawTypeTxt = "危险区域";
				$("#button1").addClass("layui-btn-danger");
				
			}
			else
			{
				this.drawTypeTxt = "任务区域";
				
				$("#button1").removeClass("layui-btn-danger");
			}
			
		},
		
	}
})
